package labox.innovation.gameserver.model.itemcontainer;

import java.util.List;
import java.util.Map;

import javolution.util.FastList;
import labox.innovation.config.Config;
import labox.innovation.db.ItemsDb;
import labox.innovation.gameserver.datatables.ArmorSetsTable;
import labox.innovation.gameserver.datatables.SkillTable;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.L2ArmorSet;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.L2Skill;
import labox.innovation.gameserver.model.L2ItemInstance.ItemLocation;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId; //import labox.innovation.gameserver.network.serverpackets.SkillCoolTime;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.ItemBouns;
import labox.innovation.gameserver.templates.item.L2Armor;
import labox.innovation.gameserver.templates.item.L2Item;
import labox.innovation.gameserver.templates.item.L2Weapon;

/**
 * 背包管理类
 * @author zjj
 *
 */
public abstract class Inventory extends ItemContainer {

	public interface PaperdollListener {
		public void notifyEquiped(int slot, L2ItemInstance inst);

		public void notifyUnequiped(int slot, L2ItemInstance inst);
	}

	public static final int PAPERDOLL_SHOULDER = 0; // 肩部 肩胛
	
	public static final int PAPERDOLL_HEAD = 1; // 头部
	
	public static final int PAPERDOLL_NECK = 2; // 项链
	
	public static final int PAPERDOLL_WING = 3; // 翅膀 或 披風

	public static final int PAPERDOLL_CHEST = 4; // 胸部
	
	public static final int PAPERDOLL_HAND = 5; // 手 武器
	
	public static final int PAPERDOLL_LBRACELET = 6; // 左手护腕
	
	public static final int PAPERDOLL_RBRACELET = 7; // 右手护腕

	public static final int PAPERDOLL_LFINGER = 8; // 左边戒指
	
	public static final int PAPERDOLL_RFINGER = 9; // 右边戒指

	public static final int PAPERDOLL_BELT = 10; //玉佩

	public static final int PAPERDOLL_FEET = 11; // 足部 脚部
	
	public static final int PAPERDOLL_LEGS = 12; // 腿部

	public static final int PAPERDOLL_DECO = 13; // 法宝

	public static final int PAPERDOLL_STRIDER = 14; // 坐骑


	// 最大背包 可使用各数 现为60. 这个值 应该写到 Config文件中去
	public static final int MAX_SLOTS = 60;

	// 当前背包可使用的背包格数
	public int _maxCurrentSoltNums = 20;

	// 插槽位 占用标记
	protected boolean[] _slots = new boolean[MAX_SLOTS + 1];

	// 最大的人物 装备槽数
	public static final int PAPERDOLL_TOTALSLOTS = 15;


	// 装备在身上的物品
	private final L2ItemInstance[] _paperdoll;

	private final List<PaperdollListener> _paperdollListeners;

	// used to quickly check for using of items of special type
	private int _wearedMask;

	// 装备 在角色身上的 装备数
	protected int _equipItemNums = 0;

	/**
	 * 获得角色身上的装备数
	 * 
	 * @return
	 */
	public int getEquipItems() {
		return _equipItemNums;
	}

	/**
	 * 获得一个空的插槽 位置 默认 返回第一个位置
	 * 
	 * @return
	 */
	public int getSlot() {
		for (int i = 0; i < _maxCurrentSoltNums; i++) {
			if (!_slots[i]) {
				_slots[i] = true;
				return i;
			}
		}

		// 背包没有空余插槽 返回-1
		return -1;
	}


	/**
	 * 正装、礼服 监听器
	 * 
	 * @author augustus
	 * 
	 */
	// final class FormalWearListener implements PaperdollListener
	// {
	// public void notifyUnequiped(int slot, L2ItemInstance item)
	// {
	// if (!(getOwner() != null && getOwner() instanceof L2PcInstance))
	// return;
	//
	// L2PcInstance owner = (L2PcInstance)getOwner();
	//
	// if (item.getItemId() == 6408)
	// owner.setIsWearingFormalWear(false);
	// }
	//
	// public void notifyEquiped(int slot, L2ItemInstance item)
	// {
	// if (!(getOwner() != null && getOwner() instanceof L2PcInstance))
	// return;
	//
	// L2PcInstance owner = (L2PcInstance)getOwner();
	//
	// // If player equips Formal Wear, unequip weapons and abort cast/attack
	// // 如果玩家穿着礼服 或 正装。 卸下武器 并且 取消施法 和 攻击
	// if (item.getItemId() == 6408)
	// owner.setIsWearingFormalWear(true);
	// else
	// {
	// if (!owner.isWearingFormalWear())
	// return;
	// }
	// }
	// }

	/**
	 * 装备改变监听
	 */
	public static final class ChangeRecorder implements PaperdollListener {
		private final Inventory _inventory;

		private final List<L2ItemInstance> _changed;

		/**
		 * Constructor of the ChangeRecorder
		 * 
		 * @param inventory
		 */
		ChangeRecorder(Inventory inventory) {
			_inventory = inventory;
			_changed = new FastList<L2ItemInstance>();
			_inventory.addPaperdollListener(this);
		}

		/**
		 * Add alteration in inventory when item equiped
		 */
		public void notifyEquiped(int slot, L2ItemInstance item) {
			if (!_changed.contains(item))
				_changed.add(item);
		}

		/**
		 * Add alteration in inventory when item unequiped
		 */
		public void notifyUnequiped(int slot, L2ItemInstance item) {
			if (!_changed.contains(item))
				_changed.add(item);
		}

		/**
		 * Returns alterations in inventory
		 * 
		 * @return L2ItemInstance[] : array of alterated items
		 */
		public L2ItemInstance[] getChangedItems() {
			return _changed.toArray(new L2ItemInstance[_changed.size()]);
		}
	}

	/**
	 * 状态 当装备 或 卸下装备 时 要增加 或 移除掉 人物身上的 状态计算函数
	 */
	final class StatsListener implements PaperdollListener {
		public void notifyUnequiped(int slot, L2ItemInstance item) {
			// System.out.println("移除角色:" + getOwner() + "装备:" + item + "相关属性");
			if( item.getEndurance() > 0 )
				getOwner().removeStatsOwner(item);
		}

		public void notifyEquiped(int slot, L2ItemInstance item) {
			// System.out.println("增加角色:" + getOwner() + "装备:" + item + "相关属性");
			if( item.getEndurance() > 0 )
				getOwner().addStatFuncs(item.getStatFuncs(getOwner()));
		}
	}

	/**
	 * 当装备 或 卸下装备 时 要增加 或 移除掉 人物 身上装备附加属性的计算公式、 如:追星、铭刻等！
	 * 
	 * @author zjj
	 * 
	 */
	final class ItemBounsListener implements PaperdollListener {
		public void notifyUnequiped(int slot, L2ItemInstance item) {
			Map<Integer, ItemBouns> temp = item.getItemBouns();
			if (temp != null && item.getEndurance() > 0) {
				// System.out.println("移除角色:" + getOwner() + "装备:" + item +
				// "附加属性");
				for (ItemBouns ib : temp.values()) {
					ib.removeBouns(getOwner());
				}
			}

		}

		public void notifyEquiped(int slot, L2ItemInstance item) {
			Map<Integer, ItemBouns> temp = item.getItemBouns();
			if ( temp != null && item.getEndurance() > 0 ) {
				// System.out.println("增加角色:" + getOwner() + "装备:" + item +
				// "附加属性");
				for (ItemBouns ib : temp.values()) {
					ib.applyBouns(getOwner());
				}
			}
		}
	}

	/**
	 * 
	 * @author augustus
	 * 
	 */
	final class ItemSkillsListener implements PaperdollListener {
		public void notifyUnequiped(int slot, L2ItemInstance item) {
			FuzePcInstance player;

			if (getOwner() instanceof FuzePcInstance)
				player = (FuzePcInstance) getOwner();
			else
				return;

			L2Skill enchant4Skill, itemSkill;
			int skillId, skillLvl;
			L2Item it = item.getItem();
			boolean update = false;

			// 如果卸下的是武器
			if (it instanceof L2Weapon) {
				// Remove augmentation bonuses on unequip
				// 移除精炼的奖励
				// if (item.isAugmented())
				// item.getAugmentation().removeBonus(player);

				// Remove skills bestowed from +4 Rapiers/Duals
				if (item.getEnchantLevel() >= 4) {
					enchant4Skill = ((L2Weapon) it).getEnchant4Skill();

					if (enchant4Skill != null) {
						player.removeSkill(enchant4Skill, false, enchant4Skill.isPassive());
						update = true;
					}
				}

				final String[] skills = ((L2Weapon) it).getSkills();

				if (skills != null) {
					for (String skillInfo : skills) {
						skillId = 0;
						skillLvl = 0;
						String[] skill = skillInfo.split("-");

						if (skill != null && skill.length == 2) {
							try {
								skillId = Integer.parseInt(skill[0]);
								skillLvl = Integer.parseInt(skill[1]);
							} catch (NumberFormatException e) {
								_log.error("Inventory.ItemSkillsListener.Weapon: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID, e );
							}
							if (skillId > 0 && skillLvl > 0) {
								itemSkill = SkillTable.getInstance().getInfo(skillId, skillLvl);

								if (itemSkill != null) {
									player.removeSkill(itemSkill, false, itemSkill.isPassive());
									update = true;
								} else
									_log.warn("Inventory.ItemSkillsListener.Weapon: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID );
							}
						}
					}
				}
			}//
			// 如果卸下的是装备
			else if (it instanceof L2Armor) {
				// Remove augmentation bonuses on unequip
				// if (item.isAugmented())
				// item.getAugmentation().removeBonus(player);

				// Remove skills bestowed from +4 armor
				if (item.getEnchantLevel() >= 4) {
					enchant4Skill = ((L2Armor) it).getEnchant4Skill();

					if (enchant4Skill != null) {
						player.removeSkill(enchant4Skill, false, enchant4Skill.isPassive());
						update = true;
					}
				}

				final String[] skills = ((L2Armor) it).getSkills();

				if (skills != null) {
					for (String skillInfo : skills) {
						skillId = 0;
						skillLvl = 0;
						String[] skill = skillInfo.split("-");

						if (skill != null && skill.length == 2) {
							try {
								skillId = Integer.parseInt(skill[0]);
								skillLvl = Integer.parseInt(skill[1]);
							} catch (NumberFormatException e) {
								_log.error("Inventory.ItemSkillsListener.Armor: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID, e);
							}
							if (skillId > 0 && skillLvl > 0) {
								itemSkill = SkillTable.getInstance().getInfo(skillId, skillLvl);

								if (itemSkill != null) {
									player.removeSkill(itemSkill, false, itemSkill.isPassive());
									update = true;
								} else
									_log.warn("Inventory.ItemSkillsListener.Armor: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID);
							}
						}
					}
				}
			}
			if (update)
				player.sendSkillList();
		}// end

		// 装备武器，装备
		public void notifyEquiped(int slot, L2ItemInstance item) {
			FuzePcInstance player;

			if (getOwner() instanceof FuzePcInstance)
				player = (FuzePcInstance) getOwner();
			else
				return;

			L2Skill enchant4Skill, itemSkill;
			int skillId, skillLvl;
			L2Item it = item.getItem();
			boolean update = false;
			boolean updateTimeStamp = false;

			if (it instanceof L2Weapon) {

				final String[] skills = ((L2Weapon) it).getSkills();

				if (skills != null) {
					for (String skillInfo : skills) {
						skillId = 0;
						skillLvl = 0;
						String[] skill = skillInfo.split("-");

						if (skill != null && skill.length == 2) {
							try {
								skillId = Integer.parseInt(skill[0]);
								skillLvl = Integer.parseInt(skill[1]);
							} catch (NumberFormatException e) {
								_log.warn("Inventory.ItemSkillsListener.Weapon: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID, e );
							}
							if (skillId > 0 && skillLvl > 0) {
								itemSkill = SkillTable.getInstance().getInfo(skillId, skillLvl);

								if (itemSkill != null) {
									player.addSkill(itemSkill, false);

									if (itemSkill.isActive()) {
	
										updateTimeStamp = true;
									}
									update = true;
								} else
									_log.warn("Inventory.ItemSkillsListener.Weapon: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID);
							}
						}
					}
				}
			} else if (it instanceof L2Armor) {
				// Apply augmentation bonuses on equip
				// if (item.isAugmented())
				// item.getAugmentation().applyBonus(player);

				final String[] skills = ((L2Armor) it).getSkills();

				if (skills != null) {
					for (String skillInfo : skills) {
						skillId = 0;
						skillLvl = 0;
						String[] skill = skillInfo.split("-");

						if (skill != null && skill.length == 2) {
							try {
								skillId = Integer.parseInt(skill[0]);
								skillLvl = Integer.parseInt(skill[1]);
							} catch (NumberFormatException e) {
								_log.warn("Inventory.ItemSkillsListener.Armor: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID, e);
							}
							if (skillId > 0 && skillLvl > 0) {
								itemSkill = SkillTable.getInstance().getInfo(skillId, skillLvl);

								if (itemSkill != null) {
									player.addSkill(itemSkill, false);

									if (itemSkill.isActive()) {
							
										updateTimeStamp = true;
									}
									update = true;
								} else
									_log.warn("Inventory.ItemSkillsListener.Armor: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID);
							}
						}
					}
				}
			}
			if (update) {
				// player.sendSkillList();

				// if (updateTimeStamp)
				// player.sendPacket(new SkillCoolTime(player));
			}
		}
	}

	/**
	 * 套装 装备 天堂2 中的套装 胸部是必须的。且套装效果 只有整套齐了 才会有效果
	 * 
	 */
	final class ArmorSetListener implements PaperdollListener {

		public void notifyEquiped(int slot, L2ItemInstance item) {
			if (!(getOwner() instanceof FuzePcInstance))
				return;

			FuzePcInstance player = (FuzePcInstance) getOwner();

			// Checks if player is wearing a chest item
			L2ItemInstance chestItem = getPaperdollItem(PAPERDOLL_CHEST);

			if (chestItem == null)
				return;

			// Checks for armorset for the equiped chest
			L2ArmorSet armorSet = ArmorSetsTable.getInstance().getSet(chestItem.getItemId());

			if (armorSet == null)
				return;

			boolean update = false;
			boolean updateTimeStamp = false;
			// Checks if equiped item is part of set
			// 检查这件装备 是否是套装的一部分
			if (armorSet.containItem(slot, item.getItemId())) {
				if (armorSet.containAll(player)) {
					int skillId, skillLvl;
					L2Skill itemSkill;
					final String[] skills = armorSet.getSkills();

					if (skills != null) {
						for (String skillInfo : skills) {
							skillId = 0;
							skillLvl = 0;
							String[] skill = skillInfo.split("-");
							if (skill != null && skill.length == 2) {
								try {
									skillId = Integer.parseInt(skill[0]);
									skillLvl = Integer.parseInt(skill[1]);
								} catch (NumberFormatException e) {
									_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID, e);
								}
								if (skillId > 0 && skillLvl > 0) {
									itemSkill = SkillTable.getInstance().getInfo(skillId, skillLvl);
									if (itemSkill != null) {
										player.addSkill(itemSkill, false);

										if (itemSkill.isActive()) {
					
											updateTimeStamp = true;
										}
										update = true;
									} else {
										_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID);
									}
								}
							}
						}
					}

					if (armorSet.containShield(player)) // has shield from set
					{
						final L2Skill shieldSkill = SkillTable.getInstance().getInfo(armorSet.getShieldSkillId(), 1);

						if (shieldSkill != null) {
							player.addSkill(shieldSkill, false);
							update = true;
						} else
							_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + armorSet.getShieldSkillId() + ".", Config.SERVER_ID);
					}

					if (armorSet.isEnchanted6(player)) // has all parts of set
					// enchanted to 6 or
					// more
					{
						final int skillId6 = armorSet.getEnchant6skillId();

						if (skillId6 > 0) {
							L2Skill skille = SkillTable.getInstance().getInfo(skillId6, 1);

							if (skille != null) {
								player.addSkill(skille, false);
								update = true;
							} else
								_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + armorSet.getEnchant6skillId() + ".",Config.SERVER_ID);
						}
					}
				} // end if (armorSet.containAll(player))

			}// end if (armorSet.containItem(slot, item.getItemId()))

			else if (armorSet.containShield(item.getItemId())) {
				if (armorSet.containAll(player)) {
					final L2Skill shieldSkill = SkillTable.getInstance().getInfo(armorSet.getShieldSkillId(), 1);

					if (shieldSkill != null) {
						player.addSkill(shieldSkill, false);
						update = true;
					} else
						_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + armorSet.getShieldSkillId() + ".", Config.SERVER_ID);
				}
			}

			if (update) {
				// player.sendSkillList();

				// if (updateTimeStamp)
				// player.sendPacket(new SkillCoolTime(player));
			}
		}

		public void notifyUnequiped(int slot, L2ItemInstance item) {
			if (!(getOwner() instanceof FuzePcInstance))
				return;

			FuzePcInstance player = (FuzePcInstance) getOwner();

			boolean remove = false;
			int skillId, skillLvl;
			L2Skill itemSkill;
			String[] skills = null;
			int shieldSkill = 0; // shield skill
			int skillId6 = 0; // enchant +6 skill

			if (slot == PAPERDOLL_CHEST) {
				L2ArmorSet armorSet = ArmorSetsTable.getInstance().getSet(item.getItemId());
				if (armorSet == null)
					return;

				remove = true;
				skills = armorSet.getSkills();
				shieldSkill = armorSet.getShieldSkillId();
				skillId6 = armorSet.getEnchant6skillId();
			} else {
				L2ItemInstance chestItem = getPaperdollItem(PAPERDOLL_CHEST);
				if (chestItem == null)
					return;

				L2ArmorSet armorSet = ArmorSetsTable.getInstance().getSet(chestItem.getItemId());
				if (armorSet == null)
					return;

				if (armorSet.containItem(slot, item.getItemId())) // removed
				// part of
				// set
				{
					remove = true;
					skills = armorSet.getSkills();
					shieldSkill = armorSet.getShieldSkillId();
					skillId6 = armorSet.getEnchant6skillId();
				} else if (armorSet.containShield(item.getItemId())) // removed
				// shield
				{
					remove = true;
					shieldSkill = armorSet.getShieldSkillId();
				}
			}

			if (remove) {
				if (skills != null) {
					for (String skillInfo : skills) {
						skillId = 0;
						skillLvl = 0;
						String[] skill = skillInfo.split("-");
						if (skill != null && skill.length == 2) {
							try {
								skillId = Integer.parseInt(skill[0]);
								skillLvl = Integer.parseInt(skill[1]);
							} catch (NumberFormatException e) {
								_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + skillInfo + ".", Config.SERVER_ID, e);
							}
							if (skillId > 0 && skillLvl > 0) {
								itemSkill = SkillTable.getInstance().getInfo(skillId, skillLvl);
								if (itemSkill != null)
									player.removeSkill(itemSkill, false, itemSkill.isPassive());
								else
									_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + skillInfo + ".",Config.SERVER_ID);
							}
						}
					}
				}

				if (shieldSkill != 0) {
					L2Skill skill = SkillTable.getInstance().getInfo(shieldSkill, 1);
					if (skill != null)
						player.removeSkill(skill, false, skill.isPassive());
					else
						_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + shieldSkill + ".", Config.SERVER_ID);
				}

				if (skillId6 != 0) {
					L2Skill skill = SkillTable.getInstance().getInfo(skillId6, 1);
					if (skill != null)
						player.removeSkill(skill, false, skill.isPassive());
					else
						_log.warn("Inventory.ArmorSetListener: Incorrect skill: " + skillId6 + ".", Config.SERVER_ID);
				}

				player.checkItemRestriction();
				// player.sendSkillList();
			}
		}
	}

	/**
	 * Constructor of the inventory
	 */
	protected Inventory() {
		_paperdoll = new L2ItemInstance[PAPERDOLL_TOTALSLOTS];
		_paperdollListeners = new FastList<PaperdollListener>();
		// addPaperdollListener(new ArmorSetListener());
		// addPaperdollListener(new ItemSkillsListener());
		addPaperdollListener(new StatsListener());
		addPaperdollListener(new ItemBounsListener());
		// addPaperdollListener(new FormalWearListener());

	}

	/**
	 * 获得装备的位置
	 * 
	 * @return
	 */
	protected abstract ItemLocation getEquipLocation();

	/**
	 * Returns the instance of new ChangeRecorder 获得一个 装备改变记录器
	 * 
	 * @return ChangeRecorder
	 */
	public ChangeRecorder newRecorder() {
		return new ChangeRecorder(this);
	}

	/**
	 * Drop item from inventory and updates database
	 * 
	 * @param process
	 *            : String Identifier of process triggering this action
	 * @param item
	 *            : L2ItemInstance to be dropped
	 * @param actor
	 *            : L2PcInstance Player requesting the item drop
	 * @param reference
	 *            : L2Object Object referencing current action like NPC selling
	 *            item or previous item in transformation
	 * @return L2ItemInstance corresponding to the destroyed item or the updated
	 *         item in inventory 从物品容器(可能是背包)中 丢一个物品 ， 更新数据库
	 */
	public L2ItemInstance dropItem(String process, L2ItemInstance item, FuzePcInstance actor, L2Object reference) {
		if (item == null)
			return null;

		synchronized (item) {
			if (!_items.contains(item))
				return null;

			removeItem(item);
			item.setOwnerId(process, 0, actor, reference);
//			item.setLocation(ItemLocation.VOID);
			item.setLocation(ItemLocation.VOID, -1);
			item.setLastChange(L2ItemInstance.REMOVED);
			
			_log.info("丢弃物品:" + process + item.getName() + "(" + item.getObjectId() + ")" + "with" + actor.getName() + "(" + actor.getObjectId() + ")" , item.getObjectId(), item.getName() );

			// item.updateDatabase();

		}
		
		return item;
		
	}

	/**
	 * Drop item from inventory by using its <B>objectID</B> and updates
	 * database
	 * 
	 * @param process
	 *            : String Identifier of process triggering this action
	 * @param objectId
	 *            : int Item Instance identifier of the item to be dropped
	 * @param count
	 *            : int Quantity of items to be dropped
	 * @param actor
	 *            : L2PcInstance Player requesting the item drop
	 * @param reference
	 *            : L2Object Object referencing current action like NPC selling
	 *            item or previous item in transformation
	 * @return L2ItemInstance corresponding to the destroyed item or the updated
	 *         item in inventory
	 */
	public L2ItemInstance dropItem(String process, long objectId, int count, FuzePcInstance actor, L2Object reference) {
		L2ItemInstance item = getItemByObjectId(objectId);
		if (item == null)
			return null;

		synchronized (item) {
			if (!_items.contains(item))
				return null;

			// Adjust item quantity and create new instance to drop
			// Directly drop entire item
			if (item.getCount() > count) {
				item.changeCount(process, -count, actor, reference);
				item.setLastChange(L2ItemInstance.MODIFIED);
				// item.updateDatabase();

				// 创建一个新的物品 ， 因为这个物品可能要掉落在地上
				// 现在物品 不需要掉落在地上 因此 不需要创建新的 物品实例, 暂时屏蔽
				// item = ItemTable.getInstance().createItem(process,
				// item.getItemId(), count, actor, reference);
				// item.updateDatabase();

				return item;
			}
		}

		return dropItem(process, item, actor, reference);
	}

	/**
	 * Adds item to inventory for further adjustments and Equip it if necessary
	 * (itemlocation defined)<BR>
	 * <BR>
	 * 
	 * @param item
	 *            : L2ItemInstance to be added from inventory 加入物品到容器 如果物品需要装备
	 *            那么装备它
	 */
	@Override
	protected void addItem(L2ItemInstance item) {
		super.addItem(item);
		//如果索引插槽不存在，且装备没有装备。
		if (item.getLocationSlot() >= 0 && !item.isEquipped() ) {
//			System.out.println(item + "插槽位置:" + item.getLocationSlot());
			_slots[item.getLocationSlot()] = true;
		}
		if (item.isEquipped())
			equipItem( item, item.getLocationSlot() );
	}

	/**
	 * Removes item from inventory for further adjustments.
	 * 
	 * @param item
	 *            : L2ItemInstance to be removed from inventory 从物品容器中删除物品 如果物品
	 *            已装备 那么卸下这个装备
	 */
	@Override
	protected boolean removeItem(L2ItemInstance item) {
		// Unequip item if equiped
		// 遍历所有可装备的地方。 查看是否已装备了 这个装备
		// 装备了 那么 卸下这个装备
		for (int i = 0; i < _paperdoll.length; i++) {
			if (_paperdoll[i] == item)
				DropPaperdollItem(i);
		}

		if (item != null && item.getLocationSlot() >= 0 && !item.isEquipped())
			restInventorySlot( item.getLocationSlot() );
		return super.removeItem(item);
	}

	/**
	 * Returns the item in the paperdoll slot 获得人物装备孔上的 装备
	 * 
	 * @return L2ItemInstance
	 */
	public L2ItemInstance getPaperdollItem(int slot) {
		return _paperdoll[slot];
	}

	/**
	 * Returns the item in the paperdoll L2Item slot
	 * 
	 * @param L2Item
	 *            slot identifier : 在L2Item中 定义的solt值
	 * @return L2ItemInstance
	 */
	public L2ItemInstance getPaperdollItemByL2ItemId(int slot) {
		switch (slot) {
		case L2Item.SLOT_NECK:
			return _paperdoll[PAPERDOLL_NECK]; // NECK 脖子
		case L2Item.SLOT_R_FINGER:
			return _paperdoll[PAPERDOLL_RFINGER]; // R_FINGER 右手戒指
		case L2Item.SLOT_L_FINGER:
			return _paperdoll[PAPERDOLL_LFINGER]; // L_FINGER 左手戒指
		case L2Item.SLOT_R_FINGER | L2Item.SLOT_L_FINGER:
			if( _paperdoll[PAPERDOLL_LFINGER] != null )
				return _paperdoll[PAPERDOLL_LFINGER];
			else 
				return _paperdoll[PAPERDOLL_RFINGER];
		case L2Item.SLOT_HEAD:
			return _paperdoll[PAPERDOLL_HEAD]; // HEAD 头部 头盔
		case L2Item.SLOT_HAND:
			return _paperdoll[PAPERDOLL_HAND]; // HAND 手 武器
		case L2Item.SLOT_CHEST:
			return _paperdoll[PAPERDOLL_CHEST]; // CHEST 胸部 胸甲
		case L2Item.SLOT_LEGS:
			return _paperdoll[PAPERDOLL_LEGS]; // LEGS 腿部 腿甲
		case L2Item.SLOT_FEET:
			return _paperdoll[PAPERDOLL_FEET]; // FEET 脚部 鞋子
		case L2Item.SLOT_SHOULDER:
			return _paperdoll[PAPERDOLL_SHOULDER]; // SHOULDER 肩部 肩胛
		case L2Item.SLOT_R_BRACELET:
			return _paperdoll[PAPERDOLL_RBRACELET]; // R_BRACELET 右手护腕
		case L2Item.SLOT_L_BRACELET:
			return _paperdoll[PAPERDOLL_LBRACELET]; // L_BRACELET 左手护腕
		case L2Item.SLOT_R_BRACELET | L2Item.SLOT_L_BRACELET:
			if( _paperdoll[PAPERDOLL_RBRACELET] != null )
				return _paperdoll[PAPERDOLL_LBRACELET];
			else
				return _paperdoll[PAPERDOLL_RBRACELET];
		case L2Item.SLOT_DECO:
			return _paperdoll[PAPERDOLL_DECO]; // DECO 法宝
		case L2Item.SLOT_BELT:
			return _paperdoll[PAPERDOLL_BELT]; // BELT 玉佩
		case L2Item.SLOT_STRIDER:
			return _paperdoll[PAPERDOLL_STRIDER]; // STRIDER 坐骑
		case L2Item.SLOT_WING:
			return _paperdoll[PAPERDOLL_WING]; // SLOT_WING 翅膀

		}
		return null;
	}

	/**
	 * Returns the ID of the item in the paperdol slot
	 * 
	 * @param slot
	 *            : int designating the slot
	 * @return int designating the ID of the item 获得 此装备孔上的物品id
	 */
	public int getPaperdollItemId(int slot) {
		L2ItemInstance item = _paperdoll[slot];
		if (item != null)
			return item.getItemId();
		return 0;
	}

	/**
	 * 获得 装备部位物品的精炼效果id
	 * 
	 * @param slot
	 * @return
	 */
	public int getPaperdollAugmentationId(int slot) {
		L2ItemInstance item = _paperdoll[slot];
		// if (item != null)
		// {
		// if (item.getAugmentation() != null)
		// return item.getAugmentation().getAugmentationId();
		// else
		// return 0;
		// }
		return 0;
	}

	/**
	 * Returns the objectID associated to the item in the paperdoll slot
	 * 
	 * @param slot
	 *            : int pointing out the slot
	 * @return int designating the objectID 获得 此装备孔上的装备id
	 */
	public long getPaperdollObjectId(int slot) {
		L2ItemInstance item = _paperdoll[slot];
		if (item != null)
			return item.getObjectId();
		return 0;
	}

	/**
	 * 获得相应插槽的装备显示id
	 * 
	 * @param slot
	 * @return
	 */
	public int getPaperdollDisplayId(int slot) {
		L2ItemInstance item = _paperdoll[slot];
		if (item != null)
			return item.getDisplayIconId();

		return -1;
	}

	/**
	 * 通过背包插槽位置 获得物品
	 * 
	 * @param slot
	 * @return
	 */
	public L2ItemInstance getItemByInventorySlot(int slot) {
		for (L2ItemInstance item : _items) {
			if (item.getLocationSlot() == slot) {
				return item;
			}
		}

		return null;
	}

	/**
	 * Adds new inventory's paperdoll listener
	 * 
	 * @param PaperdollListener
	 *            pointing out the listener
	 */
	public void addPaperdollListener(PaperdollListener listener) {
		if (Config.ASSERT)
			assert !_paperdollListeners.contains(listener);
		_paperdollListeners.add(listener);
	}

	/**
	 * Removes a paperdoll listener
	 * 
	 * @param PaperdollListener
	 *            pointing out the listener to be deleted
	 */
	public void removePaperdollListener(PaperdollListener listener) {
		_paperdollListeners.remove(listener);
	}

	/**
	 * Equips an item in the given slot of the paperdoll. <U><I>Remark :</I></U>
	 * The item <B>HAS TO BE</B> already in the inventory
	 * 
	 * @param slot
	 *            : int pointing out the slot of the paperdoll
	 * @param item
	 *            : L2ItemInstance pointing out the item to add in slot
	 * @return L2ItemInstance designating the item placed in the slot before
	 *         装备一个物品装备 在指定的装备位置
	 */
	public L2ItemInstance setPaperdollItem(int slot, L2ItemInstance item) {
		L2ItemInstance old = _paperdoll[slot];
		
		L2Character owner = getOwner();
		
		if (old != item) {
			// 已装备了物品
			// 卸下这个装备
			if (old != null) {
				// 如果卸下物品时 背包中 不能放下 一个物品
				// 那么 物品不能被卸载
				// item为空 才说明 此次只是卸载一个装备， 而不是换装
				if (!validateCapacity(1) && item == null) {
//					getOwner().sendPacket( new SystemMessage(SystemMessageId.INVENTORY_IS_FULL));
					getOwner().sendMessage("背包已满");
					return null;
				}

				_paperdoll[slot] = null;
				// Put old item from paperdoll slot to base location
				// 如果是换装 那么把卸下的物品 放到 需要装备的物品的槽位上
				if (item != null) {
					old.setLocation(getBaseLocation(), item.getLocationSlot());
//					System.out.println("待装备装备的插槽位置:" + item.getLocationSlot());
				} else
					old.setLocation(getBaseLocation(), getSlot());

				old.setLastChange(L2ItemInstance.MODIFIED);
				// Get the mask for paperdoll
				int mask = 0;
				for (int i = 0; i < PAPERDOLL_HAND; i++) {
					L2ItemInstance pi = _paperdoll[i];
					if (pi != null)
						mask |= pi.getItem().getItemMask();
				}
				_wearedMask = mask;
				// Notify all paperdoll listener in order to unequip old item in
				// slot
				for (PaperdollListener listener : _paperdollListeners) {
					if (listener == null)
						continue;

					listener.notifyUnequiped(slot, old);
				}
				
				//当item 为空时，说明 这次是卸载装备
				if( item == null )
				{
					if( owner instanceof FuzePcInstance )
					{
						//卸载装备时 需要获得 初始的外形id
						FuzePcInstance temp = (FuzePcInstance)owner;
						
						temp.getAppearance().setPaperdoll(slot, temp.getTemplate().getDefaultPaperDoll(slot) );
					}
				}

				// 每次卸下一个装备， 那么装备数 减1
//				System.out.println("卸下一个装备");
				_equipItemNums--;

			}// end if (old != null)

			// Add new item in slot of paperdoll
			// 装备新的物品装备
			if (item != null) {
				_paperdoll[slot] = item;
				if (old == null && !item.isEquipped() ) {
					// 如果不是换装 那么设置此物品 所占的背包格位空
					restInventorySlot( item.getLocationSlot() );
//					System.out.println("重置插槽位置:" + item.getLocationSlot() + "物品名:" + item.getName() );
				}
				item.setLocation(getEquipLocation(), slot);
				item.setLastChange(L2ItemInstance.MODIFIED);
				_wearedMask |= item.getItem().getItemMask();
				for (PaperdollListener listener : _paperdollListeners) {
					if (listener == null)
						continue;
					listener.notifyEquiped(slot, item);
				}

				//
				if( owner instanceof FuzePcInstance )
				{
					//通知新的展现id
					( (FuzePcInstance)owner ).getAppearance().setPaperdoll(slot, item.getDisplayIconId());
				}

				// 每次卸下一个装备， 那么装备数 减1
//				System.out.println("装备一个装备");
				_equipItemNums++;

			} // end if (item != null)
			
			//更新背包 角色属性
//			owner.sendPacket( new InventoryPcStatus( (FuzePcInstance)owner ) );

		}// end if (old != item)

		return old;

	}// setPaperdollItem(int slot, L2ItemInstance item)

	/**
	 * Return the mask of weared item 获得 装备了的物品
	 * 
	 * @return int
	 */
	public int getWearedMask() {
		return _wearedMask;
	}

	/**
	 * 获得该装备 可以装备的位置
	 * 
	 * @param item
	 * @return
	 */
	public int getSlotFromItem(L2ItemInstance item) {
		int slot = -1;
		int location = item.getLocationSlot();

		switch (location) {
		case PAPERDOLL_NECK:
			slot = L2Item.SLOT_NECK;
			break;
		case PAPERDOLL_RFINGER:
			slot = L2Item.SLOT_R_FINGER;
			break;
		case PAPERDOLL_LFINGER:
			slot = L2Item.SLOT_L_FINGER;
			break;
		case PAPERDOLL_HEAD:
			slot = L2Item.SLOT_HEAD;
			break;
		case PAPERDOLL_CHEST:
			slot = item.getItem().getBodyPart();
			break;
		case PAPERDOLL_LEGS:
			slot = L2Item.SLOT_LEGS;
			break;
		case PAPERDOLL_FEET:
			slot = L2Item.SLOT_FEET;
			break;
		case PAPERDOLL_LBRACELET:
			slot = L2Item.SLOT_L_BRACELET;
			break;
		case PAPERDOLL_RBRACELET:
			slot = L2Item.SLOT_R_BRACELET;
			break;
		case PAPERDOLL_DECO:
			slot = L2Item.SLOT_DECO; // 法宝
			break;
		// case PAPERDOLL_BELT: slot = L2Item.SLOT_BELT;
		// break;
		case PAPERDOLL_WING:
			slot = L2Item.SLOT_WING;
			break;
		}

		return slot;

	}

	/**
	 * Unequips item in body slot and returns alterations.
	 * 
	 * @param slot
	 *            : int designating the slot of the paperdoll
	 * @return L2ItemInstance[] : list of changes 卸下一个装备孔上的装备 这个函数 是从人身上卸下装备
	 *         BodySlot
	 */
	public L2ItemInstance[] unEquipItemInBodySlotAndRecord(int slot) {
		Inventory.ChangeRecorder recorder = newRecorder();

		try {
			// 卸下人身上装备孔的装备
			unEquipItemInBodySlot(slot);
			// 如果是玩家。 卸下某个装备 或 可能会影响某些技能
			// if (getOwner() instanceof L2PcInstance)
			// ((L2PcInstance)getOwner()).refreshExpertisePenalty();
		} finally {
			removePaperdollListener(recorder);
		}
		return recorder.getChangedItems();
	}

	/**
	 * Sets item in slot of the paperdoll to null value
	 * 
	 * @param pdollSlot
	 *            : int designating the slot
	 * @return L2ItemInstance designating the item in slot before change
	 *         卸下一个装备孔上的装备 可能不止人身上的装备孔
	 */
	public L2ItemInstance unEquipItemInSlot(int pdollSlot) {
		return setPaperdollItem(pdollSlot, null);
	}
	
	/**
	 * 装备在角色身上的装备掉落
	 * @param slot
	 * @return
	 */
	protected L2ItemInstance DropPaperdollItem( int slot ){
		
		L2ItemInstance old = _paperdoll[slot];
		
		L2Character owner = getOwner();
		
		if (old != null) {

			//相应位置 值空
			_paperdoll[slot] = null;

			int mask = 0;
			for (int i = 0; i < PAPERDOLL_HAND; i++) {
				L2ItemInstance pi = _paperdoll[i];
				if (pi != null)
					mask |= pi.getItem().getItemMask();
			}
			_wearedMask = mask;
			// Notify all paperdoll listener in order to unequip old item in
			// slot
			for (PaperdollListener listener : _paperdollListeners) {
				if (listener == null)
					continue;

				listener.notifyUnequiped(slot, old);
			}
			
			
			if( owner instanceof FuzePcInstance )
			{
				//卸载装备时 需要获得 初始的外形id
				FuzePcInstance temp = (FuzePcInstance)owner;
				
				temp.getAppearance().setPaperdoll(slot, temp.getTemplate().getDefaultPaperDoll(slot) );
			}
	

			// 每次卸下一个装备， 那么装备数 减1
//			System.out.println("卸下一个装备");
			_equipItemNums--;

		}// end if 
		
		return old;
		
	}

	/**
	 * Unepquips item in slot and returns alterations
	 * 
	 * @param slot
	 *            : int designating the slot
	 * @return L2ItemInstance[] : list of items altered 卸下一个装备 从装备孔上
	 */
	public L2ItemInstance[] unEquipItemInSlotAndRecord(int slot) {
		Inventory.ChangeRecorder recorder = newRecorder();

		try {
			unEquipItemInSlot(slot);
			// if (getOwner() instanceof L2PcInstance)
			// ((L2PcInstance)getOwner()).refreshExpertisePenalty();
		} finally {
			removePaperdollListener(recorder);
		}
		return recorder.getChangedItems();
	}

	/**
	 * Unequips item in slot (i.e. equips with default value)
	 * 
	 * @param slot
	 *            : int designating the slot 从人物的装备孔上 卸下一个装备
	 */
	private void unEquipItemInBodySlot(int slot) {
		if (_log.isDebugEnabled())
			_log.debug("--- unequip body slot:" + slot);

		int pdollSlot = -1;

		switch (slot) {
		case L2Item.SLOT_NECK:
			pdollSlot = PAPERDOLL_NECK;
			break;
		case L2Item.SLOT_R_FINGER:
			pdollSlot = PAPERDOLL_RFINGER;
			break;
		case L2Item.SLOT_L_FINGER:
			pdollSlot = PAPERDOLL_LFINGER;
			break;
		case L2Item.SLOT_HEAD:
			pdollSlot = PAPERDOLL_HEAD;
			break;
		case L2Item.SLOT_CHEST:
			pdollSlot = PAPERDOLL_CHEST;
			break;
		case L2Item.SLOT_LEGS:
			pdollSlot = PAPERDOLL_LEGS;
			break;
		case L2Item.SLOT_FEET:
			pdollSlot = PAPERDOLL_FEET;
			break;
		case L2Item.SLOT_HAND:
			pdollSlot = PAPERDOLL_HAND;
			break;
		case L2Item.SLOT_L_BRACELET:
			pdollSlot = PAPERDOLL_LBRACELET;
			break;
		case L2Item.SLOT_R_BRACELET:
			pdollSlot = PAPERDOLL_RBRACELET;
			break;
		// case L2Item.SLOT_BELT: pdollSlot = PAPERDOLL_BELT;
		// break;
		case L2Item.SLOT_SHOULDER:
			pdollSlot = PAPERDOLL_SHOULDER;
			break;
		case L2Item.SLOT_DECO:
			pdollSlot = PAPERDOLL_DECO;
			break;
		case L2Item.SLOT_STRIDER:
			pdollSlot = PAPERDOLL_STRIDER;
			break;
		case L2Item.SLOT_WING:
			pdollSlot = PAPERDOLL_WING;
			break;
		}
		if (pdollSlot >= 0)
			setPaperdollItem(pdollSlot, null);
	}

	/**
	 * Equips item and returns list of alterations
	 * 
	 * @param item
	 *            : L2ItemInstance corresponding to the item
	 * @return L2ItemInstance[] : list of alterations 装备一个装备
	 */
	public L2ItemInstance[] equipItemAndRecord(L2ItemInstance item) {
		Inventory.ChangeRecorder recorder = newRecorder();

		try {
			equipItem(item);
		} finally {
			removePaperdollListener(recorder);
		}
		return recorder.getChangedItems();
	}
	
	protected void equipItem(L2ItemInstance item, int slot){
		
		if (!(getOwner() instanceof FuzePcInstance)) {
			if( _log.isDebugEnabled())
				_log.debug("不是玩家 不能装备");
			return;
		}

		// 所有这个物品的是玩家
		if (getOwner() instanceof FuzePcInstance) {
			FuzePcInstance player = (FuzePcInstance) getOwner();

			// 某些 装备在Pk值 大于一定值 时 不能装备
			if (player.getPkKills() > 0 && item.getItemId() >= 7816 && item.getItemId() <= 7831) {
				_log.debug( "PK值太大 不允许装备");
				player.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_UNABLE_TO_EQUIP_THIS_ITEM_WHEN_YOUR_PK_COUNT_IS_GREATER_THAN_OR_EQUAL_TO_ONE));
				return;
			}

			if (!player.isGM() && item.isHeroItem()) {
				_log.debug("英雄物品 不允许装备");
				return;
			}
		}
		
		setPaperdollItem( slot, item );
		
	}

	/**
	 * Equips item in slot of paperdoll.
	 * 
	 * @param item
	 *            : L2ItemInstance designating the item and slot used. 装备一个装备
	 */
	public void equipItem(L2ItemInstance item) {
		if (!(getOwner() instanceof FuzePcInstance)) {
			if( _log.isDebugEnabled())
				_log.debug("不是玩家 不能装备");
			return;
		}

		// 所有这个物品的是玩家
		if (getOwner() instanceof FuzePcInstance) {
			FuzePcInstance player = (FuzePcInstance) getOwner();

			// 某些 装备在Pk值 大于一定值 时 不能装备
			if (player.getPkKills() > 0 && item.getItemId() >= 7816 && item.getItemId() <= 7831) {
				_log.debug( "PK值太大 不允许装备");
				player.sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_UNABLE_TO_EQUIP_THIS_ITEM_WHEN_YOUR_PK_COUNT_IS_GREATER_THAN_OR_EQUAL_TO_ONE));
				return;
			}

			if (!player.isGM() && item.isHeroItem()) {
				_log.debug("英雄物品 不允许装备");
				return;
			}
		}

		// 获得这个装备 的部位
		int targetSlot = item.getItem().getBodyPart();

		switch (targetSlot) {
		// 装备在手上
		case L2Item.SLOT_HAND: {
			setPaperdollItem(PAPERDOLL_HAND, item);

			break;
		}
		case L2Item.SLOT_L_FINGER:
		case L2Item.SLOT_R_FINGER:
		case L2Item.SLOT_L_FINGER | L2Item.SLOT_R_FINGER: {
			if (_paperdoll[PAPERDOLL_LFINGER] == null)
				setPaperdollItem(PAPERDOLL_LFINGER, item);
			else if (_paperdoll[PAPERDOLL_RFINGER] == null)
				setPaperdollItem(PAPERDOLL_RFINGER, item);
			else {
				setPaperdollItem(PAPERDOLL_LFINGER, null);
				setPaperdollItem(PAPERDOLL_LFINGER, item);
			}
			break;
		}
		case L2Item.SLOT_NECK:
			setPaperdollItem(PAPERDOLL_NECK, item);
			break;
		case L2Item.SLOT_CHEST:
			setPaperdollItem(PAPERDOLL_CHEST, item);
			break;
		case L2Item.SLOT_LEGS: {
			// // handle full armor
			// L2ItemInstance chest = getPaperdollItem(PAPERDOLL_CHEST);
			// if (chest != null)
			// setPaperdollItem(PAPERDOLL_CHEST, null);
			setPaperdollItem(PAPERDOLL_LEGS, item);
			break;
		}
		case L2Item.SLOT_FEET:
			setPaperdollItem(PAPERDOLL_FEET, item);
			break;
		case L2Item.SLOT_HEAD:
			setPaperdollItem(PAPERDOLL_HEAD, item);
			break;
		case L2Item.SLOT_L_BRACELET:
		case L2Item.SLOT_R_BRACELET:
		case L2Item.SLOT_L_BRACELET | L2Item.SLOT_R_BRACELET:
		{
			if( _paperdoll[PAPERDOLL_LBRACELET] == null ){
				setPaperdollItem(PAPERDOLL_LBRACELET, item);
			}else if(  _paperdoll[PAPERDOLL_RBRACELET] == null ){
				setPaperdollItem(PAPERDOLL_RBRACELET, item);
			}else{
				
				setPaperdollItem(PAPERDOLL_LBRACELET, null);
				setPaperdollItem(PAPERDOLL_LBRACELET, item);
				
			}
			
			break;
		}
//		case L2Item.SLOT_R_BRACELET:
//			setPaperdollItem(PAPERDOLL_RBRACELET, item);
//			break;
		case L2Item.SLOT_DECO: // 法宝
			// equipTalisman(item);
			setPaperdollItem(PAPERDOLL_DECO, item);
			break;
		case L2Item.SLOT_SHOULDER:
			setPaperdollItem(PAPERDOLL_SHOULDER, item);
			break;
		case L2Item.SLOT_WING:
			setPaperdollItem(PAPERDOLL_WING, item);
			break;
		case L2Item.SLOT_BELT:
			setPaperdollItem(PAPERDOLL_BELT, item);
		 	break;
		default:
			_log.warn("Unknown body slot " + targetSlot + " for Item ID:" + item.getItemId(),Config.SERVER_ID);

		}// end switch (targetSlot)
	}

	/**
	 * Refresh the weight of equipment loaded
	 */
	@Override
	protected void refreshWeight() {
		long weight = 0;

		// for (L2ItemInstance item : _items)
		// {
		// if (item != null && item.getItem() != null)
		// weight += item.getItem().getWeight() * item.getCount();
		// }
		// _totalWeight = (int)Math.min(weight, Integer.MAX_VALUE);
	}

	/**
	 * Get back items in inventory from database 从数据库中重新加载 这个容器
	 */
//	@Override
//	public void restore() {
		// Connection con = null;
		//
		// try
		// {
		// con = L2DatabaseFactory.getInstance().getConnection();
		// PreparedStatement statement =
		// con.prepareStatement("SELECT object_id, item_id, count, enchant_level, loc, loc_data, custom_type1, custom_type2, mana_left, time FROM items WHERE owner_id=? AND (loc=? OR loc=?) ORDER BY loc_data");
		// statement.setLong(1, getOwnerId());
		// statement.setString(2, getBaseLocation().name());
		// statement.setString(3, getEquipLocation().name());
		// ResultSet inv = statement.executeQuery();
		//
		// L2ItemInstance item;
		// while (inv.next())
		// {
		// item = L2ItemInstance.restoreFromDb(getOwnerId(), inv);
		//
		// if (item == null)
		// continue;
//		List<L2ItemInstance> _items = ItemsDb.retoreContainer(getOwnerId(), getBaseLocation().name());
//		for (L2ItemInstance item : _items) {
//			if (getOwner() instanceof FuzePcInstance) {
//				FuzePcInstance player = (FuzePcInstance) getOwner();
//
//				if (player.getPkKills() > 0 && item.getItemId() >= 7816 && item.getItemId() <= 7831)
//					item.setLocation(ItemLocation.INVENTORY);
//
//				if (!player.isGM() && item.isHeroItem())
//					item.setLocation(ItemLocation.INVENTORY);
//			}
//
//			FuzeWorld.getInstance().enterWorld(item);
//
//			// If stackable item is found in inventory just add to current
//			// quantity
//			if (item.isStackable() && getItemByItemId(item.getItemId()) != null)
//				addItem("Restore", item, getOwner().getActingPlayer(), null);
//			else
//				addItem(item);
//		}
//		refreshWeight();
		// }
		// inv.close();
		// statement.close();
	
		// }
		// catch (Exception e)
		// {
		// _log.warning("Could not restore inventory : " + e);
		// }
		// finally
		// {
		// try
		// {
		// con.close();
		// }
		// catch (Exception e) {}
		// }
//	}

	/**
	 * Re-notify to paperdoll listeners every equipped item 重新装备物品
	 */
	public void reloadEquippedItems() {
		int slot;

		for (L2ItemInstance item : _paperdoll) {
			if (item == null)
				continue;

			slot = item.getLocationSlot();

			for (PaperdollListener listener : _paperdollListeners) {
				if (listener == null)
					continue;

				listener.notifyUnequiped(slot, item);
				listener.notifyEquiped(slot, item);
			}
		}
	}

	/**
	 * 重新值装备插槽标记。 false表示 没物品
	 * 
	 * @param slot
	 */
	public void restInventorySlot(int slot) {
		if (slot >= 0)
			_slots[slot] = false;
	}

	/**
	 * 获得 已装备部位的所有插槽位置
	 * 
	 * @return
	 */
	public List<Integer> getAllEquipedSolts() {
		FastList<Integer> temp = new FastList<Integer>();
		for (int i = 0; i < PAPERDOLL_TOTALSLOTS; i++) {
			if (_paperdoll[i] != null) {
				temp.add(i);
			}
		}

		return temp;

	}

	/**
	 * 获得角色身上的所有可能的装备
	 * 
	 * @return
	 */
	public L2ItemInstance[] getAllEquipItems() {
		return _paperdoll;
	}

	/**
	 * 获得背包中 有物品的插槽索引
	 * 
	 * @return
	 */
	public List<Integer> getAllInventoryItemedSlots() {
		FastList<Integer> temp = new FastList<Integer>();

		for (int i = 0; i < PAPERDOLL_TOTALSLOTS; i++) {
			if (_slots[i]) {
				temp.add(i);
			}
		}

		return temp;

	}

}
